home *** CD-ROM | disk | FTP | other *** search
- Path: crchh327.rich.bnr.ca!jhalpin
- From: jhalpin@bnr.ca (Joe Halpin)
- Newsgroups: comp.lang.c++
- Subject: Re: How do I prevent deleting of an object that is still being used?
- Date: 17 Jan 1996 13:39:41 GMT
- Organization: Bell-Northern Research, Richardson, TX
- Message-ID: <4diu6t$c5n@crchh327.rich.bnr.ca>
- References: <4dg9in$s5h@utopia.hacktic.nl>
- NNTP-Posting-Host: crchh518.rich.bnr.ca
-
- In article <4dg9in$s5h@utopia.hacktic.nl>,
- Mike Tavares <MIKET@cdynamics.com> wrote:
- >
- >I have some classes:
- >
- >class menuItem
- > {
- > ...
- >
- > }
- >
- >
- >
- >class menu
- > {
- >
- > list<menuItem> theItems // the items that are on this menu.
- >
- > Register( menuItem &anItem );
- >
- > }
- >
- >
- >A menuItem can be on more than one menu. When a menu is deleted it
- >should delete all its menuItems UNLESS they are on another menu. What is
- >the typical way of setting this up. I've heard of "contact" objects and
- >"plug and socket" objects but I've never seen an implementation
- >pattern. any help is appreciated.
-
- One possibility is to move the state of a menuItem into a separate
- class, and share that state among menuItem instances.
-
- class menuItem;
-
- class menuItemState {
- friend menuItem;
- int refCount;
- char *caption;
- // whatever other member variables are used by menuItem
- };
-
- class menuItem {
- menuItemState *state;
- ...
- };
-
- The copy constructor and assignment operator for menuItem need to
- point menuItem::state at the state of the object being assigned, and
- increment the reference count. The destructor needs to decrement the
- count, and delete state when count gets to 0. This releives the menu
- from having to worry about it at all.
-
- If I remember right, this is roughly the Flyweight pattern in "Design
- Patterns", by Gamma et al. You might also be able to use their Proxy
- pattern.
-
- Joe
- --
- Joe Halpin jhalpin@bnr.ca BNR Richardson, TX (214) 684-5657
- -------------------------------------------------------------------------------
-